<?php

namespace V6;
class  C_CaptchaMobileCode extends \ResourceClass
{

    public $isActive = true;
    public $remark = '验证手机验证码';

    public function get(array $req, array $post, &$result)
    {
        try {
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if(!isset($req['token'])){
                return 'require message token';
            }
            if(!isset($req['mobile_code'])){
                return 'require message mobile_code';
            }
            $customer_handler = new \Biz\UserHandler();
            $customer_cls = $customer_handler ->getById($req['userType'],$req['uid']);
            if($customer_cls == false){
                return 'data error';
            }
            if(!is_object($customer_cls) || $customer_cls->getEnterpriseId() == false){
                return 'data error';
            }
            $token = md5($customer_cls->getId().'XIANQIAO520'); //生成token令牌
            if($req['token'] == $token){    //检测token
                //连接memcahce
                $memcache = new \Memcache();
                $con = $memcache->connect(\Util\Config::get('memcache_host', 'Memcache'), \Util\Config::get('memcache_port', 'Memcache'));
                if (!$con) {
                    return 0;
                }
                //获取缓存手机code
                $mobile_code = preg_replace('/[(\xc2\xa0)|\s]+/','', $req['mobile_code']);
                $mobile_validate_code = $memcache->get('MOBILE_VALIDATE_CODE_'.$customer_cls->getId());
                if($mobile_validate_code == $mobile_code){   //判断手机验证码
                    $memcache->delete('MOBILE_VALIDATE_CODE_'.$customer_cls->getId());
                    $result = true;
                }else{
                    return '验证失败';
                }
            }else{
                return 'TOKEN验证失败';
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }

    }
}